from Case.python_ui.Common.basepage import Basepage
import pytest, time
from Case.python_ui.Pageobject.user_product_waf import UserProductWaf
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_product_cms_locator import ProductCmsLocator
from Case.python_ui.PageLocate.user_traffic_security_monitoring_locator import TrafficSecurityMonitoringLocator
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.Test_data.login_data import Logindata
from selenium import webdriver


class UserTrafficSecurityMonitoring(Basepage):

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 点击左上角菜单按钮进入菜单页面
    def enter_menu_page(self):
        time.sleep(2)
        doc = '点击左上角菜单按钮进入菜单页面'
        self.wait_elevisble(TrafficSecurityMonitoringLocator.product_menu_button, doc)
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.product_menu_button, doc)
        time.sleep(3)
        self.wait_elevisble(TrafficSecurityMonitoringLocator.product_menu_page, doc)

    # 点击菜单Traffic Security Monitoring按钮进入Traffic Security Monitoring控制台页面
    def enter_tsm_overview_page(self):
        doc = '点击菜单Traffic Security Monitoring按钮进入Traffic Security Monitoring控制台页面'
        time.sleep(6)
        for i in range(3):
            try:
                self.scroll_bar_operation(TrafficSecurityMonitoringLocator.tsm_menu_button)
                self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_menu_button, doc)
                time.sleep(2)
                self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_menu_button, doc)
                break
            except:
                self.refresh_page()
                time.sleep(3)
                self.enter_menu_page()
                continue
        time.sleep(8)
        # 检测控制台页面信息
        self.driver.switch_to.frame(self.get_element(TrafficSecurityMonitoringLocator.into_iframe, doc))
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_tt_page_title, doc)
        self.save_screen(doc)

    # 检测Traffic Trends控制台页面信息
    def check_traffic_trends_overview_page(self):
        doc = '检测Traffic Trends控制台页面信息'
        time.sleep(3)
        self.driver.switch_to.default_content()
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_tt_title, doc)
        time.sleep(3)
        self.driver.switch_to.frame(self.get_element(TrafficSecurityMonitoringLocator.into_iframe, doc))
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_tt_page_title, doc)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_page_title, doc).text, 'Traffic Trends')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_network_title, doc).text, 'Network Traffic Trend')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inbound_title, doc).text, 'Inbound Traffic')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_outbound_title, doc).text, 'Outbound Traffic')

    # 检测Internet Border控制台页面信息
    def check_internet_border_overview_page(self):
        doc = '检测Internet Border控制台页面信息'
        time.sleep(2)
        # 进入Internet Border控制台页面
        self.driver.switch_to.default_content()
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_ib_title, doc)
        time.sleep(5)
        self.driver.switch_to.frame(self.get_element(TrafficSecurityMonitoringLocator.into_iframe, doc))
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_ib_page_title, doc)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_ib_page_title, doc).text, 'Internet Boundary Traffic Analysis')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_1hour_btn, doc).text, 'Last 1 Hour')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_24hours_btn, doc).text, 'Last 24 Hours')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_7days_btn, doc).text, 'Last 7 Days')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_statistics_title, doc).text, 'Traffic Statistics')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_list_title, doc).text, 'Traffic List')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_visits_ip_title, doc).text, 'Visits to IP')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_average_title, doc).text, 'Average Traffic')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_peak_title, doc).text, 'Peak Traffic')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator. tsm_tt_ib_list_source_tab, doc).text, 'By Source IP')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_ib_list_destination_tab, doc).text, 'By Destination IP')

    # 检测Internal Network Border控制台页面信息
    def check_internal_network_border_overview_page(self):
        doc = '检测Internal Network Border控制台页面信息'
        time.sleep(2)
        # 进入Internal Network Border控制台页面
        self.driver.switch_to.default_content()
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_inb_title, doc)
        time.sleep(5)
        self.driver.switch_to.frame(self.get_element(TrafficSecurityMonitoringLocator.into_iframe, doc))
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_inb_page_title, doc)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_inb_page_title, doc).text, 'Intranet Boundary Traffic Analysis')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_1hour_btn, doc).text, 'Last 1 Hour')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_24hours_btn, doc).text, 'Last 24 Hours')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_7days_btn, doc).text, 'Last 7 Days')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_statistics_title, doc).text, 'Traffic Statistics')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_list_title, doc).text, 'Traffic List')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_visits_ip_title, doc).text, 'Visits to IP')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_average_title, doc).text, 'Average Traffic')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_peak_title, doc).text, 'Peak Traffic')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_list_source_tab, doc).text, 'By Source IP')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_tt_inb_list_destination_tab, doc).text, 'By Destination IP')

    # 检测Packet Capture控制台页面信息
    def check_packet_capture_overview_page(self):
        doc = '检测Packet Capture控制台页面信息'
        time.sleep(2)
        # 进入Packet Capture控制台页面
        self.driver.switch_to.default_content()
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_pc_title, doc)
        time.sleep(5)
        self.driver.switch_to.frame(self.get_element(TrafficSecurityMonitoringLocator.into_iframe, doc))
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_pc_page_title, doc)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_title, doc).text, 'Packet Capture')
        # 查看Packet Capture页面默认最近时间
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_last_default_tittle, doc).text, 'Last 30 Days')
        # 点击最近时间复选框查看选项数据
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_pc_page_last_time_btn, doc)
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tsm_pc_page_last_1day_btn, doc)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_last_1day_btn, doc).text, 'Last 1 Day')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_last_7days_btn, doc).text, 'Last 7 Days')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_last_30days_btn, doc).text, 'Last 30 Days')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_custom_btn, doc).text, 'Custom')
        # 点击Packet Capture页面新建数据按钮检测新建数据页面
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_create_btn,doc).text, 'Create Packet Capture Task')
        self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tsm_pc_page_create_btn, doc)
        time.sleep(1)
        self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_create_task_page_tittle, doc)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_pc_page_create_task_page_tittle, doc).text, 'Create Packet Capture Task')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_create_task_name_tittle, doc).text, 'Task Name')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_create_maximum_bytes_tittle, doc).text, 'Maximum Bytes')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_create_duration_tittle, doc).text, 'Duration (s)')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_create_ip_tittle, doc).text, 'IP')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_create_port_tittle, doc).text, 'Port')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tsm_create_ok_btn, doc).text, 'Ok')

    # 点击菜单Threat Detection Service按钮进入Threat Detection Service控制台页面
    def enter_tds_overview_page(self):
        doc = '点击菜单Threat Detection Service按钮进入Threat Detection Service控制台页面'
        time.sleep(6)
        for i in range(3):
            try:
                self.scroll_bar_operation(TrafficSecurityMonitoringLocator.tds_menu_button)
                self.wait_elevisble(TrafficSecurityMonitoringLocator.tds_menu_button, doc)
                time.sleep(2)
                self.move_mouse_to_click_element(TrafficSecurityMonitoringLocator.tds_menu_button, doc)
                break
            except:
                self.refresh_page()
                time.sleep(3)
                self.enter_menu_page()
                continue
        time.sleep(8)
        # 检测控制台页面信息
        self.driver.switch_to.frame(self.get_element(TrafficSecurityMonitoringLocator.into_iframe, doc))
        self.wait_elevisble(TrafficSecurityMonitoringLocator.tds_page_title, doc)
        self.save_screen(doc)

    # 检测Threat Detection Service控制台页面信息
    def check_tds_overview_page(self):
        doc = '检测Threat Detection Service控制台页面信息'
        time.sleep(2)
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_page_title, doc).text, 'Overview')
        self.driver.switch_to.default_content()
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_overview_title, doc).text, 'Overview')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_security_alerts_title, doc).text, 'Security Alerts')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_attack_analysis_title, doc).text, 'Attack Analysis')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_cloud_service_check_title, doc).text, 'Cloud Service Check')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_application_whitelists_title, doc).text, 'Application Whitelists')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_assets_title, doc).text, 'Assets')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_security_reports_title, doc).text, 'Security Reports')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_security_overview_tab, doc).text, 'Security overview')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_security_screen_tab, doc).text, 'Security Screen')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_security_score_title, doc).text, 'Security Score')
        self.check_attribute(self.get_element(TrafficSecurityMonitoringLocator.tds_asset_status_title, doc).text, 'Asset Status')




